package com.campus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.entity.*;
import com.campus.entity.Clazz.ClazzTeacher;
import com.campus.mapper.*;
import com.campus.service.InfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Alaskaboo
 * @description 针对表【t_info】的数据库操作Service实现
 * @createDate 2022-11-02 19:33:37
 */
@Service
public class InfoServiceImpl extends ServiceImpl<InfoMapper, Info> implements InfoService {
    
    @Autowired
    private ClazzStudentViewMapper clazzStudentViewMapper;
    @Autowired
    private DormStudentViewMapper dormStudentViewMapper;
    @Autowired
    private ClazzTeacherMapper clazzTeacherMapper;
    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private BuildingMapper buildingMapper;

    @Override
    public IPage<Info> InfoList(Page<Info> page, Info info,String account) {

        QueryWrapper<Info> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(info.getPublishTitle())) {
            queryWrapper.like("publishTitle", info.getPublishTitle());
        }
        if (!StringUtils.isEmpty(info.getDeadline())) {
            queryWrapper.like("deadline", info.getDeadline());
        }
        if (!StringUtils.isEmpty(info.getPublishTag())) {
            queryWrapper.like("publishTag", info.getPublishTag());
        }
        if (buildingMapper.selectCount(new QueryWrapper<BuildingEntity>().eq("building_admin",account))!=0){
            infoByDorm(queryWrapper,account);
        }else{
            if(teacherMapper.selectCount(new QueryWrapper<Teacher>().eq("account",account))!=0){
                infoByInstructor(queryWrapper,account);
            }else{
                infoByStudent(queryWrapper,account);
            }
        }
        queryWrapper.orderByAsc("id");
        return baseMapper.selectPage(page, queryWrapper);
    }
    private void infoByStudent(QueryWrapper<Info> queryWrapper,String account){
        List<String> creator = new ArrayList<>();

        //查询account对应的宿管账号
        DormStudentView dorm = dormStudentViewMapper.selectOne(new QueryWrapper<DormStudentView>().eq("account", account));
        creator.add(dorm.getBuildingAdmin());


        //查询account对应的班级账号
        ClazzStudentView currentAccount = clazzStudentViewMapper.selectOne(new QueryWrapper<ClazzStudentView>().eq("account", account));
        Long classId = currentAccount.getClassId();
        //        List<ClazzStudentView> students = clazzStudentViewMapper.selectList(new QueryWrapper<ClazzStudentView>().eq("class_id", currentAccount.getClassId()));
//        for(ClazzStudentView csv:students){
//            creator.add(String.valueOf(csv.getAccount()));
//        }


        //根据当前账号的班级号，查询班级号对应的辅导员
        ClazzTeacher clazzTeacher= clazzTeacherMapper.selectOne(new QueryWrapper<ClazzTeacher>().eq("class_id", classId));
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("id", clazzTeacher.getTeacherId()));
        creator.add(teacher.getAccount());

        queryWrapper.in("create_by",creator);
    }



    private void infoByDorm(QueryWrapper<Info> queryWrapper,String account){
        queryWrapper.eq("create_by",account);
    }

    private void infoByInstructor(QueryWrapper<Info> queryWrapper,String account){
        Teacher teacher = teacherMapper.selectOne(new QueryWrapper<Teacher>().eq("account", account));
        List<ClazzTeacher> clazzList = clazzTeacherMapper.selectList(new QueryWrapper<ClazzTeacher>().eq("teacher_id", teacher.getId()));
        List<String> stus = new ArrayList<>();
        for(ClazzTeacher ct:clazzList){
            List<ClazzStudentView> list = clazzStudentViewMapper.selectList(new QueryWrapper<ClazzStudentView>().eq("class_id", ct.getClassId()));
            for(ClazzStudentView cst:list){
                stus.add(cst.getAccount());
            }
        }
        stus.add(account);
        queryWrapper.in("create_by",stus);
    }
}




